home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / termutil.0 / termutil / termutils-2.0 / tcutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-03  |  3.8 KB  |  156 lines

  1. /* Utility functions to work with termutils.
  2.    Copyright (C) 1995 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21.  
  22. #include <tcutil.h>
  23. #include <termcap.h>
  24. #define ERROR_EXIT 5        /* Real error or signal. */
  25.  
  26. /* We would appreciate it if somebody using a system with
  27.    termio but no termios nor sgtty could add termio support
  28.    for the following macros - junio@twinsun.com */
  29.  
  30. #if HAVE_TERMIOS_H
  31. #include <termios.h>
  32. typedef struct termios tty_mode;
  33. #ifdef HAVE_TCGETATTR
  34. #define GET_TTY_MODE(fd,buf) tcgetattr ((fd), (buf))
  35. #define SET_TTY_MODE(fd,buf) tcsetattr ((fd), TCSANOW, (buf))
  36. #define GET_OSPEED(buf) cfgetospeed ((buf))
  37. #else /* not HAVE_TCGETATTR */
  38. #define GET_TTY_MODE(fd,buf) ioctl ((fd), TCGETS, (buf))
  39. #define SET_TTY_MODE(fd,buf) ioctl ((fd), TCSETS, (buf))
  40. #define GET_OSPEED(buf) ((buf)->c_cflag & CBAUD)
  41. #endif /* not HAVE_TCGETATTR */
  42. #define DISABLE_OPOST(buf) ((buf)->c_oflag &= ~OPOST)
  43. #if !defined(XTABS) && defined(TAB3)
  44. #define XTABS TAB3
  45. #endif /* !defined(XTABS) && defined(TAB3) */
  46. #if defined(XTABS)
  47. #define ENABLE_XTABS(buf) ((buf)->c_oflag |= XTABS)
  48. #define DISABLE_XTABS(buf) ((buf)->c_oflag &= ~XTABS)
  49. #else /* not defined(XTABS) */
  50. #define ENABLE_XTABS(buf)    /* empty */
  51. #define DISABLE_XTABS(buf)    /* empty */
  52. #endif /* not defined(XTABS) */
  53. #else /* not HAVE_TERMIOS_H */
  54. #if HAVE_SGTTY_H
  55. #include <sgtty.h>
  56. typedef struct sgttyb tty_mode;
  57. #define GET_TTY_MODE(fd,buf) gtty ((fd), (buf))
  58. #define SET_TTY_MODE(fd,buf) stty ((fd), (buf))
  59. #define GET_OSPEED(buf) (buf)->sg_ospeed
  60. #define DISABLE_OPOST(buf) ((buf)->sg_flags &= ~(XTABS|LCASE|CRMOD))
  61. #define ENABLE_XTABS(buf) ((buf)->sg_flags |= XTABS)
  62. #define DISABLE_XTABS(buf) ((buf)->sg_flags &= ~XTABS)
  63. #else /* not HAVE_SGTTY_H */
  64. #define NO_TTY_CONTROL
  65. #endif /* not HAVE_SGTTY_H */
  66. #endif /* not HAVE_TERMIOS_H */
  67.  
  68. #include <signal.h>
  69.  
  70. #ifdef NO_TTY_CONTROL
  71. void
  72. translations_off ()
  73. {
  74.   ;                /* empty */
  75. }
  76.  
  77. void
  78. restore_translations ()
  79. {
  80.   ;                /* empty */
  81. }
  82.  
  83. void
  84. enable_xtabs ()
  85. {
  86.   ;                /* empty */
  87. }
  88.  
  89. void
  90. disable_xtabs ()
  91. {
  92.   ;                /* empty */
  93. }
  94. #else
  95. static tty_mode old_modes, new_modes;
  96.  
  97. static RETSIGTYPE
  98. signal_handler ()
  99. {
  100.   restore_translations ();
  101.   exit (ERROR_EXIT);
  102. }
  103. /* Turn off expansion of tabs into spaces, saving the old
  104.    terminal settings first.
  105.    Also set OSPEED.  */
  106.  
  107. void
  108. translations_off ()
  109. {
  110.   if (isatty (1))
  111.     {
  112.       GET_TTY_MODE (1, &old_modes);
  113.  
  114.       if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  115.     signal (SIGINT, signal_handler);
  116.       if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  117.     signal (SIGHUP, signal_handler);
  118.       if (signal (SIGQUIT, SIG_IGN) != SIG_IGN)
  119.     signal (SIGQUIT, signal_handler);
  120.       signal (SIGTERM, signal_handler);
  121.  
  122.       new_modes = old_modes;
  123.  
  124.       DISABLE_OPOST (&new_modes);
  125.       SET_TTY_MODE (1, &new_modes);
  126.       ospeed = GET_OSPEED (&old_modes);
  127.     }
  128.   else
  129.     ospeed = 0;
  130. }
  131.  
  132. /* Restore the old terminal settings.  */
  133.  
  134. void
  135. restore_translations ()
  136. {
  137.   SET_TTY_MODE (1, &old_modes);
  138. }
  139.  
  140. void
  141. enable_xtabs ()
  142. {
  143.   new_modes = old_modes;
  144.   ENABLE_XTABS (&new_modes);
  145.   SET_TTY_MODE (1, &new_modes);
  146. }
  147.  
  148. void
  149. disable_xtabs ()
  150. {
  151.   new_modes = old_modes;
  152.   DISABLE_XTABS (&new_modes);
  153.   SET_TTY_MODE (1, &new_modes);
  154. }
  155. #endif
  156.